package com.fly.caihong.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fly.caihong.R;
import com.fly.caihong.base.BaseActivity;
import com.fly.caihong.model.AuctionDetailItem;
import com.fly.caihong.model.ZppmItem;
import com.fly.caihong.utils.ApUtils;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.GlideUtils;
import com.fly.caihong.utils.StatusBarUtils;
import com.fly.caihong.utils.ToastUtils;

public class AuctionDetailActivity extends BaseActivity {

    @Override
    protected int getLayout() {
        return R.layout.activity_auction_detail;
    }

    public static void launchActivity(Context context, String id) {
        Intent itt = new Intent(context, AuctionDetailActivity.class);
        itt.putExtra("id", id);
        context.startActivity(itt);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setColor(this, ActivityCompat.getColor(this, R.color.theme_bg), 0);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setStatusBarStyle() {
    }

    @Override
    protected void initViews() {
        super.initViews();
        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                NestedScrollView nestedScrollView = findViewById(R.id.nest_scroll_view);
                nestedScrollView.scrollTo(0, 0);
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        AuctionDetailItem auctionDetailItem = DataHandler.convertJsonData("home/auction/detail"
                , getIntent().getStringExtra("id") + ".json", AuctionDetailItem.class);
        if (auctionDetailItem == null) {
            ToastUtils.showLong("拍卖详情数据为空");
            return;
        }
        RecyclerView recyclerView = findViewById(R.id.rv_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new BaseQuickAdapter<ZppmItem, BaseViewHolder>(R.layout.layout_auction_zppm_item, auctionDetailItem.getZppm()) {


            @Override
            protected void convert(BaseViewHolder helper, ZppmItem item) {
                ImageView ivImage = helper.getView(R.id.iv_image);
                GlideUtils.loadSrcImage(ApUtils.getApp(), ivImage, item.getImage());
                helper.setText(R.id.tv_title, item.getTitle()).setText(R.id.tv_pm_number, item.getNumber())
                        .setText(R.id.tv_price, item.getPrice()).setText(R.id.tv_date, item.getDate());
            }

        });
        TextView tvAuthor = findViewById(R.id.tv_author);
        TextView tvContent = findViewById(R.id.tv_content);
        ImageView ivProfile = findViewById(R.id.iv_profile);
        tvAuthor.setText(auctionDetailItem.getAuthor());
        tvContent.setText(auctionDetailItem.getContent());
        GlideUtils.loadFullImageFitWidth(this, ivProfile, auctionDetailItem.getPhoto());
    }
}
